import sys
from PyQt5.QtWidgets import QApplication, QWidget, QListWidget, QPushButton, QVBoxLayout, QListWidgetItem
from PyQt5.QtGui import QIcon, QPixmap

class ImageSelectionApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('Image Selection')

        # Create a list widget to display images
        self.listWidget = QListWidget()
        self.addImages()

        # Create a push button to confirm the selected images
        self.confirmButton = QPushButton('Confirm')
        self.confirmButton.clicked.connect(self.confirmImages)

        # Create a layout and add the list widget and push button
        layout = QVBoxLayout()
        layout.addWidget(self.listWidget)
        layout.addWidget(self.confirmButton)

        # Set the layout of the window
        self.setLayout(layout)

        # Set the geometry of the window
        self.setGeometry(300, 300, 400, 200)
        self.show()

    def addImages(self):
        # Define a list of image names
        images = [
            'image1.jpg',
            'image2.jpg',
            'image3.jpg',
            'image4.jpg',
            'image5.jpg',
        ]

        # Iterate through the list of images and add them to the list widget
        for image in images:
            item = QListWidgetItem()
            icon = QIcon()
            icon.addPixmap(QPixmap(image), QIcon.Normal, QIcon.Off)
            item.setIcon(icon)
            item.setText(image)
            self.listWidget.addItem(item)

    def confirmImages(self):
        # Get the selected items in the list widget
        selectedItems = self.listWidget.selectedItems()
        for item in selectedItems:
            print('Selected image:', item.text())

        # Clear the selection after the images are confirmed
        self.listWidget.clearSelection()
    
    def initUI(self):
        # Set the window title of the application
        self.setWindowTitle('Image Selection')

        # Create a grid layout for the user interface
        self.gridLayout = QGridLayout()
        # Add images to the user interface
        self.addImages()

        # Create a button for confirming the images
        self.confirmButton = QPushButton('Confirm')
        # Connect the button's clicked signal to the confirmImages method
        self.confirmButton.clicked.connect(self.confirmImages)

        # Add the list widget and the button to the grid layout
        self.gridLayout.addWidget(self.listWidget, 0, 0)
        self.gridLayout.addWidget(self.confirmButton, 1, 0)

        # Set the grid layout for the user interface
        self.setLayout(self.gridLayout)

        # Set the geometry of the window
        self.setGeometry(300, 300, 400, 200)
        # Show the window
        self.show()
    def initUI(self):
        self.setWindowTitle('Image Selection')

        self.gridLayout = QGridLayout()
        self.addImages()

        self.confirmButton = QPushButton('Confirm')
        self.confirmButton.clicked.connect(self.confirmImages)

        self.gridLayout.addWidget(self.listWidget, 0, 0)
        self.gridLayout.addWidget(self.confirmButton, 1, 0)

        self.setLayout(self.gridLayout)

        self.setGeometry(300, 300, 400, 200)
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    imageSelectionApp = ImageSelectionApp()
    sys.exit(app.exec_())

